<!doctype html>



  


<html class="theme-next muse use-motion" lang="zh-hk">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Java," />








  <link rel="shortcut icon" type="image/x-icon" href="/blog/favicon.ico?v=5.1.0" />






<meta name="description" content="Java 编程的 151 个建议 (一)
莫让常量蜕变成变量
注意可变形参的使用
别让 Null 值影响可变形参方法的使用
一些自增的陷阱
静态导入
序列化
参考文献">
<meta property="og:type" content="article">
<meta property="og:title" content="Java 编程的 151 个建议 (一)">
<meta property="og:url" content="http://jocens.oschina.io/blog/2017/02/14/151-1/index.html">
<meta property="og:site_name" content="Averson">
<meta property="og:description" content="Java 编程的 151 个建议 (一)
莫让常量蜕变成变量
注意可变形参的使用
别让 Null 值影响可变形参方法的使用
一些自增的陷阱
静态导入
序列化
参考文献">
<meta property="og:updated_time" content="2017-02-28T03:00:34.984Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Java 编程的 151 个建议 (一)">
<meta name="twitter:description" content="Java 编程的 151 个建议 (一)
莫让常量蜕变成变量
注意可变形参的使用
别让 Null 值影响可变形参方法的使用
一些自增的陷阱
静态导入
序列化
参考文献">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://jocens.oschina.io/blog/2017/02/14/151-1/"/>





  <title> Java 编程的 151 个建议 (一) | Averson </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-hk">

  














  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/blog/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">Averson</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
    
      <p class="site-subtitle"></p>
    
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首頁
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            歸檔
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            標籤
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="http://jocens.oschina.io/blog/blog/2017/02/14/151-1/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="Averson">
    <meta itemprop="description" content="">
    <meta itemprop="image" content="/blog/images/avatar.gif">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="Averson">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="Averson" src="">
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Java 编程的 151 个建议 (一)
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">發表於</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2017-02-14T15:00:50+08:00">
                2017-02-14
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/blog/2017/02/14/151-1/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2017/02/14/151-1/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="Java-编程的-151-个建议-一"><a href="#Java-编程的-151-个建议-一" class="headerlink" title="Java 编程的 151 个建议 (一)"></a>Java 编程的 151 个建议 (一)</h1><ul>
<li>莫让常量蜕变成变量</li>
<li>注意可变形参的使用</li>
<li>别让 Null 值影响可变形参方法的使用</li>
<li>一些自增的陷阱</li>
<li>静态导入</li>
<li>序列化</li>
<li>参考文献</li>
</ul>
<a id="more"></a>
<h2 id="莫让常量蜕变成变量"><a href="#莫让常量蜕变成变量" class="headerlink" title="莫让常量蜕变成变量"></a>莫让常量蜕变成变量</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="class"><span class="keyword">interface</span> <span class="title">Const</span> </span>&#123;</div><div class="line">  <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> RANDOM_NUMBER = <span class="keyword">new</span> Random().nextInt();</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">Client</span> </span>&#123;</div><div class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</div><div class="line">    print(Const.RANDOM_NUMBER);</div><div class="line">  &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="注意可变形参的使用"><a href="#注意可变形参的使用" class="headerlink" title="注意可变形参的使用"></a>注意可变形参的使用</h2><p>基本数据类型优先调用, 所以下面例子中, 优先调用第一个 <code>calc()</code> 方法.</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>&#123;</div><div class="line"></div><div class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</div><div class="line">    <span class="keyword">int</span> calc = calc(<span class="number">1</span>, <span class="number">2</span>);</div><div class="line">    System.out.println(calc);</div><div class="line">  &#125;</div><div class="line"></div><div class="line"></div><div class="line">  <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">calc</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span> </span>&#123;</div><div class="line">    <span class="keyword">return</span> a + b;</div><div class="line">  &#125;</div><div class="line"></div><div class="line"></div><div class="line">  <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">calc</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span>... b)</span> </span>&#123;</div><div class="line">    <span class="keyword">int</span> result = a;</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i : b) &#123;</div><div class="line">      result += i;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> result;</div><div class="line">  &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="别让-Null-值影响可变形参方法的使用"><a href="#别让-Null-值影响可变形参方法的使用" class="headerlink" title="别让 Null 值影响可变形参方法的使用"></a>别让 Null 值影响可变形参方法的使用</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">Ambiguous method call. Both</div><div class="line">calc(int,int...) in Main and</div><div class="line">calc(int,String...) in Main match</div></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">calc</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span>... b)</span> </span>&#123;&#125;</div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">calc</span><span class="params">(<span class="keyword">int</span> a, String... b)</span> </span>&#123;&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</div><div class="line">  calc(<span class="number">1</span>, <span class="keyword">null</span>);</div><div class="line">  <span class="comment">// correctly modified</span></div><div class="line">  String[] params = <span class="keyword">null</span>;</div><div class="line">  calc(<span class="number">1</span>, params);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<blockquote>
<p>即建议遇见可变参数方法调用时, 进行传递参数使用明确的类型数组进行传递.</p>
</blockquote>
<h2 id="一些自增的陷阱"><a href="#一些自增的陷阱" class="headerlink" title="一些自增的陷阱"></a>一些自增的陷阱</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</div><div class="line">  <span class="keyword">int</span> count = <span class="number">0</span>;</div><div class="line">  <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">10</span>; i++) &#123;</div><div class="line">    count = count++;</div><div class="line">  &#125;</div><div class="line">  System.out.println(count);</div><div class="line">&#125;</div><div class="line"><span class="comment">// output: 0</span></div></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// mock count++ logic</span></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">mockSelfIncreasing1</span><span class="params">(<span class="keyword">int</span> count)</span> </span>&#123;</div><div class="line">  <span class="keyword">int</span> temp = <span class="number">0</span>;</div><div class="line">  count = count + <span class="number">1</span>;</div><div class="line">  <span class="keyword">return</span> temp;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">// mock ++count logic</span></div><div class="line"><span class="function"><span class="keyword">static</span> <span class="keyword">int</span> <span class="title">mockSelfIncreasing2</span><span class="params">(<span class="keyword">int</span> count)</span> </span>&#123;</div><div class="line">  count = count + <span class="number">1</span>;</div><div class="line">  <span class="keyword">int</span> temp = count;</div><div class="line">  <span class="keyword">return</span> temp;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</div><div class="line">  <span class="keyword">int</span> count = <span class="number">0</span>;</div><div class="line">  <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">10</span>; i++) &#123;</div><div class="line">    count = mockSelfIncreasing1(count); <span class="comment">// 0</span></div><div class="line">    count = mockSelfIncreasing2(count); <span class="comment">// 10</span></div><div class="line">  &#125;</div><div class="line">  System.out.println(count);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="静态导入"><a href="#静态导入" class="headerlink" title="静态导入"></a>静态导入</h2><ul>
<li>不使用 <code>*</code> 通配符</li>
<li>方法名明确、清晰可以使用静态导入</li>
<li>不要在静态导入后在本类覆写静态类中的内容(属性或方法)</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> <span class="keyword">static</span> org.junit.Assert.*;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MainTest</span> </span>&#123;</div><div class="line"></div><div class="line">  <span class="meta">@Test</span> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testMethod</span><span class="params">()</span> </span>&#123;</div><div class="line">    assertEquals(<span class="string">"foo"</span>, <span class="string">"foo"</span>);</div><div class="line">  &#125;</div><div class="line"></div><div class="line">  <span class="comment">// don't do that</span></div><div class="line">  <span class="function"><span class="keyword">static</span> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title">assertEquals</span><span class="params">(Object expected, Object actual)</span> </span>&#123;</div><div class="line">    System.out.println(<span class="string">"testMethod() call this"</span>);</div><div class="line">  &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="序列化"><a href="#序列化" class="headerlink" title="序列化"></a>序列化</h2><ul>
<li><code>serialVersionUID</code> 是用来作为序列化与反序列化是否一致的标准</li>
<li>序列化不包括静态属性</li>
<li>序列化的时候只记录两部分信息<ul>
<li>类描述信息 (不记录方法、构造函数、静态变量的具体实现)</li>
<li>非瞬间( <code>transient</code> )和非静态( <code>static</code> )的实例变量</li>
</ul>
</li>
<li>反序列化 <code>final</code> 不会重新赋值的情况:<ul>
<li>构造函数为 <code>final</code> 变量赋值</li>
<li>方法返回值赋值</li>
<li><code>final</code> 修饰的属性不是基本类型</li>
</ul>
</li>
</ul>
<blockquote>
<p>部分简单的代码忽略</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SerializationUtil</span> </span>&#123;</div><div class="line"></div><div class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">serializedToDisk</span><span class="params">(String filename, Object o)</span> </span>&#123;</div><div class="line">    <span class="keyword">try</span> &#123;</div><div class="line">      FileOutputStream fos = <span class="keyword">new</span> FileOutputStream(<span class="keyword">new</span> File(filename));</div><div class="line">      ObjectOutputStream oos = <span class="keyword">new</span> ObjectOutputStream(fos);</div><div class="line">      oos.writeObject(o);</div><div class="line">      oos.flush();</div><div class="line">      oos.close();</div><div class="line">    &#125; <span class="keyword">catch</span> (IOException e) &#123;</div><div class="line">      e.printStackTrace();</div><div class="line">    &#125;</div><div class="line">  &#125;</div><div class="line"></div><div class="line"></div><div class="line">  <span class="keyword">public</span> <span class="keyword">static</span> &lt;T&gt; <span class="function">T <span class="title">deserializedFromDisk</span><span class="params">(String filename, Class&lt;T&gt; cls)</span> </span>&#123;</div><div class="line">    <span class="keyword">try</span> &#123;</div><div class="line">      FileInputStream fis = <span class="keyword">new</span> FileInputStream(<span class="keyword">new</span> File(filename));</div><div class="line">      ObjectInputStream ois = <span class="keyword">new</span> ObjectInputStream(fis);</div><div class="line">      <span class="keyword">return</span> cls.cast(ois.readObject());</div><div class="line">    &#125; <span class="keyword">catch</span> (IOException e) &#123;</div><div class="line">      e.printStackTrace();</div><div class="line">    &#125; <span class="keyword">catch</span> (ClassNotFoundException e) &#123;</div><div class="line">      e.printStackTrace();</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> <span class="keyword">null</span>;</div><div class="line">  &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="serialVersionUID-是用来作为序列化与反序列化是否一致的标准"><a href="#serialVersionUID-是用来作为序列化与反序列化是否一致的标准" class="headerlink" title="serialVersionUID 是用来作为序列化与反序列化是否一致的标准"></a><code>serialVersionUID</code> 是用来作为序列化与反序列化是否一致的标准</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Person</span> <span class="keyword">implements</span> <span class="title">Serializable</span> </span>&#123;</div><div class="line">  <span class="keyword">private</span> String name;</div><div class="line">  <span class="keyword">private</span> Integer age;</div><div class="line"></div><div class="line">  <span class="comment">// getter and setter</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MainTest</span> </span>&#123;</div><div class="line"></div><div class="line">  <span class="meta">@Before</span> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setup</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="keyword">new</span> File(FILE_NAME).delete();</div><div class="line">    Person person = <span class="keyword">new</span> Person();</div><div class="line">    person.setName(<span class="string">"Averson"</span>);</div><div class="line">    person.setAge(<span class="number">20</span>);</div><div class="line">    SerializationUtil.serializedToDisk(FILE_NAME, person);</div><div class="line">  &#125;</div><div class="line"></div><div class="line"></div><div class="line">  <span class="meta">@Test</span> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testUIDDifferent</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="comment">// 1. Person init field no has serialVersionUID</span></div><div class="line">    <span class="comment">// 2. serializedToDisk(); (Person 1.0)</span></div><div class="line">    <span class="comment">// 3. Person add a address field, but serialVersionUID constant(Person 2.0)</span></div><div class="line">    <span class="comment">// 4. deserializedFromDisk() execute failure and throw InvalidClassException</span></div><div class="line">    Person person = SerializationUtil.deserializedFromDisk(FILE_NAME, Person.class);</div><div class="line"></div><div class="line">    <span class="comment">// below code don't execute because above code throw exception</span></div><div class="line">    assertEquals(person.getName(), <span class="string">"Averson"</span>);</div><div class="line">    assertEquals(person.getAddress(), <span class="keyword">null</span>);</div><div class="line">  &#125;</div><div class="line"></div><div class="line"></div><div class="line">  <span class="meta">@Test</span> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testUIDConsistent</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="comment">// 1. Person init field has serialVersionUID</span></div><div class="line">    <span class="comment">// 2. serializedToDisk();</span></div><div class="line">    <span class="comment">// 3. Person add a address field, but serialVersionUID constant</span></div><div class="line">    <span class="comment">// 4. deserializedFromDisk() execute success, but address field is null</span></div><div class="line">    Person person = SerializationUtil.deserializedFromDisk(FILE_NAME, Person.class);</div><div class="line">    assertEquals(person.getName(), <span class="string">"Averson"</span>);</div><div class="line">    assertEquals(person.getAddress(), <span class="keyword">null</span>);</div><div class="line">  &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="反序列化-final-不会重新赋值的情况"><a href="#反序列化-final-不会重新赋值的情况" class="headerlink" title="反序列化 final 不会重新赋值的情况"></a>反序列化 <code>final</code> 不会重新赋值的情况</h3><blockquote>
<p>构造函数赋值</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span> <span class="keyword">implements</span> <span class="title">Serializable</span> </span>&#123;</div><div class="line">  <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">long</span> serialVersionUID = <span class="number">1L</span>;</div><div class="line">  <span class="keyword">public</span> <span class="keyword">final</span> String name = <span class="string">"Averson"</span>;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</div><div class="line">  File file = <span class="keyword">new</span> File(FILE_NAME);</div><div class="line">  <span class="keyword">if</span> (file.exists()) &#123;</div><div class="line">    Person p = SerializationUtil.deserializedFromDisk(FILE_NAME, Person.class);</div><div class="line">    System.out.println(p.getName()); <span class="comment">// outpout: Averson</span></div><div class="line">  &#125;</div><div class="line">  Person person = <span class="keyword">new</span> Person();</div><div class="line">  SerializationUtil.serializedToDisk(FILE_NAME, person);</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">// modified Person</span></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span> <span class="keyword">implements</span> <span class="title">Serializable</span> </span>&#123;</div><div class="line">  <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">long</span> serialVersionUID = <span class="number">1L</span>;</div><div class="line">  <span class="keyword">public</span> <span class="keyword">final</span> String name = <span class="string">"A"</span>;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">// execute main method</span></div><div class="line"><span class="comment">// output: A</span></div></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div></pre></td><td class="code"><pre><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span> <span class="keyword">implements</span> <span class="title">Serializable</span> </span>&#123;</div><div class="line">  <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">long</span> serialVersionUID = <span class="number">1L</span>;</div><div class="line">  <span class="keyword">public</span> <span class="keyword">final</span> String name;</div><div class="line"></div><div class="line">  Person() &#123;</div><div class="line">    name = <span class="string">"Averson"</span>;</div><div class="line">  &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</div><div class="line">  File file = <span class="keyword">new</span> File(FILE_NAME);</div><div class="line">  <span class="keyword">if</span> (file.exists()) &#123;</div><div class="line">    Person p = SerializationUtil.deserializedFromDisk(FILE_NAME, Person.class);</div><div class="line">    System.out.println(p.getName()); <span class="comment">// outpout: Averson</span></div><div class="line">  &#125;</div><div class="line">  Person person = <span class="keyword">new</span> Person();</div><div class="line">  SerializationUtil.serializedToDisk(FILE_NAME, person);</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">// modified Person</span></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span> <span class="keyword">implements</span> <span class="title">Serializable</span> </span>&#123;</div><div class="line">  <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">long</span> serialVersionUID = <span class="number">1L</span>;</div><div class="line">  <span class="keyword">public</span> <span class="keyword">final</span> String name;</div><div class="line"></div><div class="line">  Person() &#123;</div><div class="line">    name = <span class="string">"A"</span>;</div><div class="line">  &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">// execute main method</span></div><div class="line"><span class="comment">// output: Averson</span></div></pre></td></tr></table></figure>
<h2 id="参考文献"><a href="#参考文献" class="headerlink" title="参考文献"></a>参考文献</h2><p><a href="#">编写高质量代码-改善Java程序的151个建议</a></p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/Java/" rel="tag"># Java</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2017/01/14/hello-world/" rel="next" title="Averson">
                <i class="fa fa-chevron-left"></i> Averson
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2017/02/14/Dagger2 浅析/" rel="prev" title="Dagger2 浅析">
                Dagger2 浅析 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2017/02/14/151-1/"
           data-title="Java 编程的 151 个建议 (一)" data-url="http://jocens.oschina.io/blog/blog/2017/02/14/151-1/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目錄
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            本站概覽
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/blog/images/avatar.gif"
               alt="Averson" />
          <p class="site-author-name" itemprop="name">Averson</p>
          <p class="site-description motion-element" itemprop="description"></p>
        </div>
        <nav class="site-state motion-element">
        
          
            <div class="site-state-item site-state-posts">
              <a href="/blog/archives">
                <span class="site-state-item-count">4</span>
                <span class="site-state-item-name">文章</span>
              </a>
            </div>
          

          

          
            <div class="site-state-item site-state-tags">
              <a href="/blog/tags">
                <span class="site-state-item-count">3</span>
                <span class="site-state-item-name">標籤</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Java-编程的-151-个建议-一"><span class="nav-number">1.</span> <span class="nav-text">Java 编程的 151 个建议 (一)</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#莫让常量蜕变成变量"><span class="nav-number">1.1.</span> <span class="nav-text">莫让常量蜕变成变量</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#注意可变形参的使用"><span class="nav-number">1.2.</span> <span class="nav-text">注意可变形参的使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#别让-Null-值影响可变形参方法的使用"><span class="nav-number">1.3.</span> <span class="nav-text">别让 Null 值影响可变形参方法的使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#一些自增的陷阱"><span class="nav-number">1.4.</span> <span class="nav-text">一些自增的陷阱</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#静态导入"><span class="nav-number">1.5.</span> <span class="nav-text">静态导入</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#序列化"><span class="nav-number">1.6.</span> <span class="nav-text">序列化</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#serialVersionUID-是用来作为序列化与反序列化是否一致的标准"><span class="nav-number">1.6.1.</span> <span class="nav-text">serialVersionUID 是用来作为序列化与反序列化是否一致的标准</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#反序列化-final-不会重新赋值的情况"><span class="nav-number">1.6.2.</span> <span class="nav-text">反序列化 final 不会重新赋值的情况</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#参考文献"><span class="nav-number">1.7.</span> <span class="nav-text">参考文献</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Averson</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 強力驅動
</div>

<div class="theme-info">
  主題 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.0"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"averson"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    
    <script src="/blog/lib/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/blog/js/src/hook-duoshuo.js"></script>
  













  
  

  

  

  

  


</body>
</html>
